/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package controller

import (
	"encoding/json"
	"fmt"
	"github.com/mjiulee/lego/utils"
	//wxcard "github.com/chanxuehong/wechat/mp/card"
	wxacitveuserform "github.com/chanxuehong/wechat/mp/card/membercard/activateuserform"
	wxcardqr "github.com/chanxuehong/wechat/mp/card/qrcode"
	testwhitelist "github.com/chanxuehong/wechat/mp/card/testwhitelist"
	"github.com/mjiulee/lego"
	"github.com/valyala/fasthttp"
	"saas_sys_base/common"
	. "saas_sys_base/modules/merchantCard/models"
	. "saas_sys_base/modules/merchantCard/service"
	//. "saas_sys_base/modules/merchantCard/service/WxCard_service"
)

type MchCardApiCtrl struct {
	mchCardApiService MchCardApiService
}

func init() {
	vc := new(MchCardApiCtrl)
	_ctrlList = append(_ctrlList, vc)
}

func (vc *MchCardApiCtrl) CtrlInfo() string {
	return "MchCardApiCtrl"
}

func (vc *MchCardApiCtrl) RoutInit() {
	//lego.APIPOSTv2("/api/wechat/card/create_card2", vc.CreateCard2, false)
	lego.APIPOST("/api/mod/mchcard/card/create", vc.CreateCard, true)

	lego.APIPOST("/api/mod/mchcard/card/batchget", vc.BatchGet, true)
	lego.APIPOST("/api/mod/mchcard/card/get", vc.GetCard, true)
	lego.APIPOST("/api/mod/mchcard/card/testwhitelist", vc.TestWhileList, true)
	lego.APIPOST("/api/mod/mchcard/card/membercard/activateuserform/set", vc.SetActivateUserForm, true)

	//lego.APIPOST("/api/mod/mchcard/card/fetch_user_card", vc.FetchUserCard, true)

	lego.APIPOST("/api/mod/mchcard/card/create_membercard_with_level", vc.CreateMemberCard2, true)
	lego.APIPOST("/api/mod/mchcard/card/get_membercard_detail", vc.GetMemberCardDetail, true)
	lego.APIPOST("/api/mod/mchcard/card/fetch_user_member_card", vc.FetchUserMemberCard, true)
	lego.APIPOST("/api/mod/mchcard/card/update_member_card", vc.UpdateMemberCard, true)
	lego.APIPOST("/api/mod/mchcard/card/update_user_member_card", vc.UpdateUserMemberCard, true)

	lego.APIGET("/api/mod/mchcard/card/get_membercard_by_tenant", vc.GetMemberCardByTenant, true)
	lego.APIPOST("/api/mod/mchcard/card/del_card", vc.DelCard, true) //这个是一个调试接口，不对外开放
	lego.APIPOST("/api/mod/mchcard/test/card/add_balance", vc.AddBalance, true) //这个是一个调试接口，不对外开放

	lego.APIPOST("/api/mod/mchcard/card/get_user_member_list_by_tenant", vc.GetUserMemberListByTenant, true)
	lego.APIPOST("/api/mod/mchcard/card/get_user_member_card_detail", vc.GetUserMemberCardDetail, true)
	lego.APIPOST("/api/mod/mchcard/card/mch_modify_user_card_balance", vc.MchModifyUserCardBalance, true)
	lego.APIPOST("/api/mod/mchcard/card/del_user_card", vc.DelUserCard, true)
	lego.APIPOST("/api/mod/mchcard/card/list_user_member_card_account_log", vc.ListUserMemberCardAccountLog, true)
}


type CreateCardRequest struct {
	MchCardUnionMsg
}

type CreateCardResp struct {
	MchCardId int64 `json:"mcn_card_id,string"`
	WxCardId string `json:"wx_card_id"`
}

func (vc *MchCardApiCtrl) CreateCard(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"



	var request CreateCardRequest
	var resp CreateCardResp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		//使用后端预先配置的卡参数作为配置卡数据
		/*err = UpdateCardParaByCardPara(&request.MchCardUnionMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}*/

		mchMemberCard, err := vc.mchCardApiService.CreateCardByWho(appwho, &request.MchCardUnionMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.MchCardId = mchMemberCard.Id
		resp.WxCardId = mchMemberCard.MemberCard.BaseInfo.CardId
		response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)

}





func (vc *MchCardApiCtrl) BatchGet(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"
	type Request struct {
		//wxcard.BatchGetQuery
		Page int `json:"page"`
		Psize int `json:"psize"`
	}
	type Resp struct {
		//wxcard.BatchGetResult
		Total int64 `json:"total"`
		CardList []MchMemberCard `json:"card_list"`

	}

	var request Request
	var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		who := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		// TODO 需要修改接口参数
		result, total, err := vc.mchCardApiService.BatchGetByWho(who, request.Page, request.Psize)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.CardList = result
		resp.Total = total
		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}

type GetCardRequest struct {
	MchCardId int64 `json:"mch_card_id"`
}

type GetCardResp struct {
	//wxcard *wxcard.Card `json:"wx_card,omitempty"`
	MchMemberCard *MchMemberCard `json:"mch_member_card,omitempty"`
}

func (vc *MchCardApiCtrl) GetCard(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	var request GetCardRequest
	var resp GetCardResp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		who := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}
		// TODO 需要修改接口参数
		mchMemberCard, err := vc.mchCardApiService.GetCardByWho(who, request.MchCardId)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.MchMemberCard = mchMemberCard
		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}

func (vc *MchCardApiCtrl) CreateCardQrCode(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		wxcardqr.CreateQrCodeReqPara
	}

	type Resp struct {
		*wxcardqr.QrcodeInfo
		ShowQrUrl string `json:"show_qr_url"`
	}

	var request Request
	var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		who := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}
		// TODO 需要修改接口参数
		result, err := vc.mchCardApiService.CreateQrCodeByWho(*who, &request.CreateQrCodeReqPara)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.QrcodeInfo = result

		resp.ShowQrUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + result.Ticket
		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}

func (vc *MchCardApiCtrl) TestWhileList(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		testwhitelist.SetParameters
	}

	type Resp struct {
	}

	var request Request
	var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		who := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}
		// TODO 需要修改接口参数
		err = vc.mchCardApiService.TestWhileListByWho(*who, &request.SetParameters)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		//resp. = result
		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}

func (vc *MchCardApiCtrl) SetActivateUserForm(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		wxacitveuserform.ActiveuserFormPara
	}

	type Resp struct {
	}

	var request Request
	var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		who := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}
		// TODO 需要修改接口参数
		err = vc.mchCardApiService.SetActivateUserForm(*who, &request.ActiveuserFormPara)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		//resp. = result
		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}
type FetchUserCardRequest struct {
	MchCardId int64 `json:"mch_card_id,string"`

	MemberInfo `json:"member_info"`
	//Birthday time.Time `json:""`
	//Phone string
}

type FetchUserCardResp struct {
	//MchCardId int64 `json:"mch_card_id"`
	MchUserCardId int64 `json:"user_card_id,string"`
	//CardId int64 `json:"card_id"`
	WxUserCardCode string `json:"wx_user_card_code,omitempty"`
}
//领取卡（卡类型为商户卡才使用此接口，微信类型的卡领卡时，微信会推送领卡消息，所以微信类型的卡再领卡消息处理函数处处理）
//术语UserCard和Card，UserCard代表用户领取的卡，而card表示商家创建的卡模板
func (vc *MchCardApiCtrl) FetchUserMemberCard(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	var request FetchUserCardRequest
	var resp FetchUserCardResp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		who := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		isValid := utils.MobilePhoneValidate(request.Phone)
		if !isValid {
			code = -1
			msg = "电话号码格式不正确"
			break
		}

		// TODO 需要修改接口参数
		mchUserCard, err := vc.mchCardApiService.FetchCardByWho(who, request.MchCardId, &request.MemberInfo)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}


		resp.MchUserCardId = mchUserCard.Id
		userCardId, err := utils.StringToInt64( mchUserCard.UserCardCode)
		if err != nil {//若userCardCode不为int64的string，则可认为是兼容微信卡模式
			resp.WxUserCardCode = mchUserCard.UserCardCode
		} else {
			if userCardId != mchUserCard.Id { //若userCardCode与mchUserCard.Id不等，则可认为是兼容微信卡模式
				resp.MchUserCardId = mchUserCard.Id
			}

		}

		response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)

}

type CreateMemberCard2Request struct {
	MchCardId int64 `json:"mch_card_id,string,omitempty"`
	//LevelNo int `json:"level_no"`
	MemberLevelConf MemberLevelConf `json:"member_level_conf"`
}

type CreateMemberCard2Resp struct {
	MchCardId int64 `json:"mch_card_id,string"`
	WxCardId string `json:"wx_card_id,omitempty"`
}

func (vc *MchCardApiCtrl) CreateMemberCard2(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	var request CreateMemberCard2Request
	var resp CreateMemberCard2Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}


		//使用后端预先配置的卡参数作为配置卡数据
		/*err = UpdateCardParaByCardPara(&request.MchCardUnionMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}*/

		mchMemberCard, err := vc.mchCardApiService.CreateCard2ByWho(appwho,
			                                                             request.MchCardId,
			                                                             &request.MemberLevelConf,
			                                                             CARD_TEMPLATE_ID_MEMBER_CARD_100)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.MchCardId = mchMemberCard.Id
		if mchMemberCard.CardMode == CARD_MODE_MCH_WECHAT ||
			mchMemberCard.CardMode == CARD_MODE_WECHAT {
			resp.WxCardId = mchMemberCard.BaseInfo.CardId //只有支持微信卡模式，才会返回wx card id
		}

		response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)

}

func (vc *MchCardApiCtrl) UpdateMemberCard(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	var request CreateMemberCard2Request
	var resp CreateMemberCard2Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}


		//使用后端预先配置的卡参数作为配置卡数据
		/*err = UpdateCardParaByCardPara(&request.MchCardUnionMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}*/

		mchMemberCard, err := vc.mchCardApiService.UpdateMemberCard2ByWho(appwho,
			request.MchCardId,
			&request.MemberLevelConf)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.MchCardId = mchMemberCard.Id
		if mchMemberCard.CardMode == CARD_MODE_MCH_WECHAT ||
			mchMemberCard.CardMode == CARD_MODE_WECHAT {
			resp.WxCardId = mchMemberCard.BaseInfo.CardId //只有支持微信卡模式，才会返回wx card id
		}
		response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)

}

func (vc *MchCardApiCtrl) UpdateUserMemberCard(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		UserCardId int64 `json:"user_card_id,string"`
		//MchCardId int64 `json:",string"`

		MemberInfo MemberInfo `json:"member_info"`
	}

	type Response struct {

	}

	var request Request
	//var resp Response

	for {
		sessionData := common.GetSessionData(&ctxExt)
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		isSuccess := utils.MobilePhoneValidate(request.MemberInfo.Phone)
		if !isSuccess {
			code = -1
			msg = "输入电话号码格式不正确"
			break
		}
		//使用后端预先配置的卡参数作为配置卡数据
		/*err = UpdateCardParaByCardPara(&request.MchCardUnionMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}*/

		err = vc.mchCardApiService.UpdateMemberInfo(appwho, request.UserCardId, &request.MemberInfo)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)

}

type GetMemberCardDetailRequest struct {
	MchCardId int64 `json:"mch_card_id,string"`
}

type GetMemberCardDetailResp struct {
	MchCardId int64 `json:"mch_card_id,string"`
	MchMemberCard *MchMemberCard `json:"mch_member_card"`
}

func (vc *MchCardApiCtrl) GetMemberCardDetail(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	var request GetMemberCardDetailRequest
	var resp GetMemberCardDetailResp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}


		//使用后端预先配置的卡参数作为配置卡数据
		/*err = UpdateCardParaByCardPara(&request.MchCardUnionMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}*/

		mchMemberCard, err := vc.mchCardApiService.CreateMemberCardDetailByWho(appwho, request.MchCardId)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.MchCardId = mchMemberCard.Id
		resp.MchMemberCard = mchMemberCard
		response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)

}

func (vc *MchCardApiCtrl) GetMemberCardByTenant(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {

	}

	type Response struct {
		*GetMemberCardDetailResp `json:",omitempty"`
		MemberCardList []int64 `json:"member_card_list,omitempty"`
	}
	//var request Request
	var resp Response

	for {
		sessionData := common.GetSessionData(&ctxExt)
		appwho := sessionData.AppTokenWho

		/*requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}*/


		//使用后端预先配置的卡参数作为配置卡数据
		/*err = UpdateCardParaByCardPara(&request.MchCardUnionMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}*/

		cardlist, total, err := vc.mchCardApiService.ListMemberCardByWho(appwho, 0, 10) //由于目前规定一个租户只能创建一张卡（有不同等级的卡），所以psize配置成2就可以
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		list := make([]int64, 0, len(cardlist))
		for _, v := range cardlist {
			list = append(list, v.Id)
		}

		if total >= 2 { //由于每个租户只能配置一张卡，total为2说明创建卡的逻辑有问题

			code = 200
			msg = "每个租户不能超过1张卡"
			resp.MemberCardList = list
			response["data"] = resp
			break
		}

		if total == 1 {
			mchMemberCard, err := vc.mchCardApiService.CreateMemberCardDetailByWho(appwho, cardlist[0].Id)
			if err != nil {
				code = -1
				msg = err.Error()
				break
			}

            resp.GetMemberCardDetailResp = new(GetMemberCardDetailResp)
            resp.MemberCardList = list
			resp.MchCardId = mchMemberCard.Id
			resp.MchMemberCard = mchMemberCard
			response["data"] = resp
			break

		} else {
			//这里逻辑就是total为0的逻辑
			resp.MemberCardList = list
			response["data"] = resp
			break
		}

		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)

}

func (vc *MchCardApiCtrl) DelCard(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
        MchCardId int64 `json:"mch_card_id,string"`
	}

	type Response struct {

	}

	var request Request
	//var resp Response

	for {
		//sessionData := common.GetSessionData(&ctxExt)
		//appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}


		//使用后端预先配置的卡参数作为配置卡数据
		/*err = UpdateCardParaByCardPara(&request.MchCardUnionMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}*/

		err = vc.mchCardApiService.DelCard(request.MchCardId) //
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)
}

func (vc *MchCardApiCtrl) AddBalance(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"


	type Request struct {
		UserCardId int64 `json:"user_card_id,string"`
		CardAddBalance CardAddBalance `json:"card_add_balance"`
	}

	type Response struct {

	}

	var request Request
	//var resp Response

	for {
		sessionData := common.GetSessionData(&ctxExt)
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}


		//使用后端预先配置的卡参数作为配置卡数据
		/*err = UpdateCardParaByCardPara(&request.MchCardUnionMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}*/

		err = vc.mchCardApiService.MchModifyUserCardBalance(appwho, request.UserCardId, request.CardAddBalance) //
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)
}


func (vc *MchCardApiCtrl) GetUserMemberListByTenant(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		common.PageParam
	}

	type UserMemberCardBriefDto struct {
		UserCardId int64 `json:",string"`
		MchCardId int64 `json:",string"`

		MemberInfo MemberInfo

	}
	type Response struct {
		UserCardList []*UserMemberCardBriefDto `json:"user_card_list"`
		Count int64 `json:"count"`
	}

	var request Request
	var resp Response

	for {
		sessionData := common.GetSessionData(&ctxExt)
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		if err := lego.Validate(request); err != nil {
			code = 100
			msg = err.Error()
			break
		}
		/*const (
			//最近到店、会员等级、优质会员、近30天生日、30天未来
			MEMBER_LIST_ORDER_FEILD_RENENTLY_ARRIVED = "recently_arrived_to_shop" //最近到店
			MEMBER_LIST_ORDER_FEILD_MEMBER_LEVEL = "member_level" //会员等级
			MEMBER_LIST_ORDER_FEILD_PREMIUM_MEMBER = "premium_member" //优质会员
			MEMBER_LIST_ORDER_FEILD_BIRTHDAY_IN_30_DAYS = "birthday_in_30_days" //近30天生日
			MEMBER_LIST_ORDER_FEILD_NOT_ARRIVED_IN_30_DAYS = "not_arrived_in_30_days" //近30天生日
		)*/
		if request.OrderField != MEMBER_LIST_ORDER_FEILD_NULL &&
		    request.OrderField != MEMBER_LIST_ORDER_FEILD_RENENTLY_ARRIVED &&
			request.OrderField != MEMBER_LIST_ORDER_FEILD_MEMBER_LEVEL_NO &&
			request.OrderField != MEMBER_LIST_ORDER_FEILD_PREMIUM_MEMBER &&
			request.OrderField != MEMBER_LIST_ORDER_FEILD_BIRTHDAY_IN_30_DAYS &&
			request.OrderField != MEMBER_LIST_ORDER_FEILD_NOT_ARRIVED_IN_30_DAYS {
			code = 100
			msg = fmt.Sprintf("not support OrderField<%s>", request.OrderField)
			break
		}

		if request.Order != common.ORDER_NULL && request.Order != common.ORDER_ASC && request.Order != common.ORDER_DESC {
			code = 100
			msg = fmt.Sprintf("not support order<%s>", request.Order)
			break
		}

		//使用后端预先配置的卡参数作为配置卡数据
		/*err = UpdateCardParaByCardPara(&request.MchCardUnionMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}*/

		resultlist, total, err := vc.mchCardApiService.ListMchUserMemberCardByTenant(appwho.TenantId,
			                                                                          request.Keyword,
			                                                                          request.OrderField, request.Order, request.Page, request.Psize) //
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.UserCardList = make([]*UserMemberCardBriefDto, 0)
		for _, v := range resultlist {
			cardBrief := UserMemberCardBriefDto{}
			cardBrief.UserCardId = v.Id
			cardBrief.MchCardId = v.MchCardId
			cardBrief.MemberInfo = v.MemberInfo

			resp.UserCardList = append(resp.UserCardList, &cardBrief)
		}
		resp.Count = total
		response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)
}

func (vc *MchCardApiCtrl) GetUserMemberCardDetail(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		//TenantId int64 `json:"tenant_id,string"`
		UserCardId int64 `json:"user_card_id,string"`
	}


	type Response struct {
		UserMemberCard MchUserMemberCard
		MemberCard MchMemberCard
	}

	var request Request
	var resp Response

	for {
		//sessionData := common.GetSessionData(&ctxExt)
		//appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		if err := lego.Validate(request); err != nil {
			code = 100
			msg = err.Error()
			break
		}
		//使用后端预先配置的卡参数作为配置卡数据
		/*err = UpdateCardParaByCardPara(&request.MchCardUnionMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}*/

		userCard := vc.mchCardApiService.GetMchUserMemberCardById(request.UserCardId) //
		if userCard == nil {
			code = -1
			msg = fmt.Sprintf("没找到用户会员卡<%d>", request.UserCardId)
			break
		}

		memberCard := vc.mchCardApiService.GetMchMemberCardById(userCard.MchCardId) //
		if memberCard == nil {
			code = -1
			msg = fmt.Sprintf("没找到会员卡<%d>", userCard.MchCardId)
			break
		}

		resp.UserMemberCard = *userCard
		resp.MemberCard = *memberCard
		response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)
}

func (vc *MchCardApiCtrl) MchModifyUserCardBalance(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		//TenantId int64 `json:"tenant_id,string"`
		UserCardId int64 `json:"user_card_id,string"`
		CardAddBalance CardAddBalance `json:"card_add_balance"`
	}



	type Response struct {
		UserMemberCard MchUserMemberCard
		MemberCard MchMemberCard

	}

	var request Request
	//var resp Response

	for {
		sessionData := common.GetSessionData(&ctxExt)
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		if err := lego.Validate(request); err != nil {
			code = 100
			msg = err.Error()
			break
		}
		//使用后端预先配置的卡参数作为配置卡数据
		/*err = UpdateCardParaByCardPara(&request.MchCardUnionMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}*/

		request.CardAddBalance.BusinessType = BusinessType_MchRechargeForUser //这个接口专门用于商家给用户充值
		err = vc.mchCardApiService.MchModifyBalance(appwho, request.UserCardId, request.CardAddBalance) //
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}


		/*resp.UserMemberCard = *userCard
		resp.MemberCard = *memberCard
		response["data"] = resp*/
		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)
}

func (vc *MchCardApiCtrl) DelUserCard(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		//TenantId int64 `json:"tenant_id,string"`
		UserCardId int64 `json:"user_card_id,string"`
	}



	type Response struct {


	}

	var request Request
	//var resp Response

	for {
		//sessionData := common.GetSessionData(&ctxExt)
		//appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		if err := lego.Validate(request); err != nil {
			code = 100
			msg = err.Error()
			break
		}
		//使用后端预先配置的卡参数作为配置卡数据
		/*err = UpdateCardParaByCardPara(&request.MchCardUnionMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}*/

		err = vc.mchCardApiService.DelUserCard(request.UserCardId) //
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}


		/*resp.UserMemberCard = *userCard
		resp.MemberCard = *memberCard
		response["data"] = resp*/
		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)
}

func (vc *MchCardApiCtrl) ListUserMemberCardAccountLog(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		UserCardId int64 `json:"user_card_id,string" vd:"$>0"`
		BusinessType []BusinessType `json:"business_type"`
		common.PageParam
	}

	type Response struct {
		List []MemberCardAccountLog
		Total int64
	}

	var request Request
	var resp Response

	for {
		//sessionData := common.GetSessionData(&ctxExt)
		//appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		if err := lego.Validate(request); err != nil {
			code = 100
			msg = err.Error()
			break
		}

		resp.List, resp.Total, err = vc.mchCardApiService.ListMemberCardAccountLogByUserCardId(request.UserCardId, request.BusinessType, request.PageParam) //
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}


		response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg

	ctxExt.JSON(200, response)
}